package se.energimyndigheten.lampguiden;

import org.json.JSONArray;
import org.json.JSONException;

import se.energimyndigheten.lampguiden.R;

public class LightSource {

    public static final int IMAGE_BASE = 0;
    public static final int IMAGE_WIRE = 1;
    public static final int IMAGE_GLOW = 2;
    public static final int IMAGE_RADIANCE = 3;

    private static final int[] IMAGES = new int[] { R.drawable.ls_store27matt, R.drawable.ls_store27klar,
                    R.drawable.ls_store27halo, R.drawable.ls_litene14matt, R.drawable.ls_litene14klar,
                    R.drawable.ls_litene14halo, R.drawable.ls_krone14klar, R.drawable.ls_krone14matt,
                    R.drawable.ls_krone14halo, R.drawable.ls_spotstift, R.drawable.ls_spotbajonett,
                    R.drawable.ls_halostift, R.drawable.ls_halostiftogla, R.drawable.lysror, R.drawable.lagenergi };
    private static final int[] IMAGES_GLOW_WIRE = new int[] { -1, R.drawable.ls_store27klar_trad, -1, -1,
                    R.drawable.ls_litene14klar_trad, -1, R.drawable.ls_krone14klar_trad, -1, -1, -1, -1, -1, -1, -1, -1 };
    private static final int[] IMAGES_GLOW = new int[] { R.drawable.ls_store27matt_glod,
                    R.drawable.ls_store27klar_glod, R.drawable.ls_store27halo_glod_inre,
                    R.drawable.ls_litene14matt_glod, R.drawable.ls_litene14klar_glod,
                    R.drawable.ls_litene14halo_glod_inre, R.drawable.ls_krone14klar_glod,
                    R.drawable.ls_krone14matt_glod, R.drawable.ls_krone14halo_glod_inre,
                    R.drawable.spotstift_glod, R.drawable.spotbajonett_glod, -1, -1, -1, -1 };
    private static final int[] IMAGES_RADIANCE = new int[] { R.drawable.ls_store27halo_glod_yttre,
                    R.drawable.ls_store27halo_glod_yttre, R.drawable.ls_store27halo_glod_yttre,
                    R.drawable.ls_litene14halo_glod_yttre, R.drawable.ls_litene14halo_glod_yttre,
                    R.drawable.ls_litene14halo_glod_yttre, R.drawable.ls_krone14halo_glod_yttre,
                    R.drawable.ls_krone14halo_glod_yttre, R.drawable.ls_krone14halo_glod_yttre, -1, -1, -1, -1, -1, -1 };
    
    
    private int mId;
    private int[] mEffects;
    private String mCaption;
    private String mLampType = "LampTypeUndefined";
    
    private String mName;
    private String mDocument;
    private int mMaxLumen;
    private int mLifeExpetancy;
    private JSONArray mPrices;
    private double mEfficiency;
    private String mType;
    private int mMyPrice;
    private int mMySavings;
    private int mYears;
    private JSONArray mReplaceWith;
    private String mEnergyClass;
    private int mEnergyRank;

    public LightSource() {
        mLampType = "LampTypeUndefined";
    }
    
    public LightSource(String name, String document, int maxLumen, int lifeExpectancy, JSONArray prices, double efficiency, String type, int years, JSONArray replace_with, String energyClass, int energyRank) {
        mName = name;
        mDocument = document;
        mMaxLumen = maxLumen;
        mLifeExpetancy = lifeExpectancy;
        mPrices = prices;
        mEfficiency = efficiency;
        mType = type;
        mYears = years;
        mReplaceWith = replace_with;
        mEnergyClass = energyClass;
        mEnergyRank = energyRank;
    }
    
    public Boolean hasReplacement(String candidateType) {
        if (candidateType==null)
            return false;
        try {
            for (int i=0; i < mReplaceWith.length(); i++) {
                String replacement = mReplaceWith.getString(i);
                if (candidateType.equals(replacement))
                    return true;
            }
        } catch (JSONException e) {
        }
        return false;
    }
    
    public int getYears() {
        return mYears;
    }

    public int getEnergyRank() {
        return mEnergyRank;
    }
    
    public String getDocument() {
        return mDocument;
    }
    
    public double getPower() {
        return mEfficiency;
    }

    public int getPrice(int lumen) {
        int price=0;
        try {
            for (int p=0; p<mPrices.length(); p+=2) {
                int lumenPrice = mPrices.getInt(p);
                int validUpToLumen=0;
                if (p==0)
                    price=lumenPrice;
                if (p+1 < mPrices.length())
                    validUpToLumen = mPrices.getInt(p+1);
                else
                    return lumenPrice; // Last will do
                if (lumen<=validUpToLumen)
                    return lumenPrice;
                    
            }
        } catch (JSONException e) { // Fallback to default
            e.printStackTrace();
        }
        return price;
    }
    
    public int getMyPrice() {
        return mMyPrice;
    }

    public void setMyPrice(int myPrice) {
        mMyPrice = myPrice;
    }
    
    public int getMySavings() {
        return mMySavings;
    }    
    
    public void setMySavings(int mySavings) {
        mMySavings = mySavings;
    }
    

    public double getEffiency() {
        return mEfficiency;
    }

    public int getMaxLumen() {
        return mMaxLumen;
    }

    public int getLifeExpetancy() {
        return mLifeExpetancy;
    }

    public String getName() {
        return mName;
    }

    
    
    public LightSource(int id, String lampTypeString, String caption, int[] effects) {
        mId = id;
        mCaption = caption;
        mLampType = lampTypeString;
        mEffects = effects;
    }

    @Override
    public String toString() {

        return LightSource.class.getSimpleName() + " : " + mId + " : " + mCaption;
    }

    public int getId() {
        return mId;
    }

    public void setId(int id) {
        mId = id;
    }
    
    public String getLampType() {
        return mLampType;
    }
    
    public void setLampType(String lampType) {
        mLampType = lampType;
    }

    public String getCaption() {
        return mCaption;
    }

    public void setCaption(String caption) {
        mCaption = caption;
    }

    public int[] getEffects() {
        return mEffects;
    }
    
    public String getType() {
        return mType;
    }
    
    public String getEnergyClass() {
        return mEnergyClass;
    }

    public void setEffects(int[] effects) {
        mEffects = effects;
    }

    public int[] getImages() {
        return new int[] { IMAGES[mId], IMAGES_GLOW_WIRE[mId], IMAGES_GLOW[mId], IMAGES_RADIANCE[mId] };
    }
}